﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
    public class ContentRatingDao :IDaoInterface<ContentRating, ContentRatingCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #region IDaoInterface<ContentRating, ContentRatingCriteria> Members
        public bool Insert(ContentRating ContentRatingEntity)
        {
            bool IsOperationSuccessful = false;
            
                if (ContentRatingEntity.EntityState == EntityState.New)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContentRating_Insert";
                    CmdObj.CommandType = CommandType.StoredProcedure;
                    
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingContentKey", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingContentKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingValue", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingValue);
                    //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingDate", ParameterDirection.Input, DbType.DateTime, ContentRatingEntity.ContentRatingDate);
                    if (ContentRatingEntity.ContentRatingProviderUserKey != -1)
                    {
                        DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderUserKey", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingProviderUserKey);
                    }
                    else
                    {
                        DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderUserKey", ParameterDirection.Input, DbType.Int32, DBNull.Value);
                    }
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderEmail", ParameterDirection.Input, DbType.String, ContentRatingEntity.ContentRatingProviderEmail);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderName", ParameterDirection.Input, DbType.String, ContentRatingEntity.ContentRatingProviderName);


                    DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ContentRatingEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecord = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecord == 1)
                    {
                        ContentRatingEntity.ContentRatingKey = Convert.ToInt32(CmdObj.Parameters["@ContentRatingKey"].Value);
                        ContentRatingEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                        ContentRatingEntity.Sys_DateOfModification = ContentRatingEntity.Sys_DateOfCreation;
                        ContentRatingEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }
                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {

                    throw new Exception("For Inserting new ContentRating entity data to database,[EntityState] attribute should be [New]");
                }
            
            return IsOperationSuccessful;
        }

        public bool Update(ContentRating ContentRatingEntity)
        {
            bool IsOperationSuccessful = false;
            
                if (ContentRatingEntity.EntityState == EntityState.Changed)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Dasideen_ContentRating_Update";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingKey", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingContentKey", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingContentKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingValue", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingValue);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingDate", ParameterDirection.Input, DbType.DateTime, ContentRatingEntity.ContentRatingDate);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderUserKey", ParameterDirection.Input, DbType.Int32, ContentRatingEntity.ContentRatingProviderUserKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderName", ParameterDirection.Input, DbType.String, ContentRatingEntity.ContentRatingProviderName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingProviderEmail", ParameterDirection.Input, DbType.String, ContentRatingEntity.ContentRatingProviderEmail);

                    DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ContentRatingEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecords = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        ContentRatingEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                        ContentRatingEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }

                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {
                    throw new Exception("For Updating existing ContentRating entity data to database, [EntityState] attribute should be [Changed]");
                }
            
            return IsOperationSuccessful;
        }

        public bool Delete(ContentRating DataEntity)
        {
            throw new NotImplementedException();
        }

        public ContentRating GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<ContentRating> GetAll()
        {
            throw new NotImplementedException();
        }

        public List<ContentRating> GetByCriteria(ContentRatingCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }
        #endregion

        public List<ContentRating> GetByContentKey(int ContentRatingContentKey)
        {
            List<ContentRating> _ContentRatingList = new List<ContentRating>();
           
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentRating_GetContentRatingByContentKey";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentRatingContentKey", ParameterDirection.Input, DbType.Int32, ContentRatingContentKey);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        ContentRating _TempContentRating = new ContentRating(); //IMPORTANT NOTE:- Create instance only when required.
                        this.TranslateContentRatingFieldsFromReaderToEntity(_TempContentRating, _DataReader);
                        _ContentRatingList.Add(_TempContentRating);
                    }
                }
                CmdObj.Dispose();
                DbConn.Close();
            
            return _ContentRatingList;
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateContentRatingFieldsFromReaderToEntity(ContentRating pContentRating, DbDataReader pDbDataReader)
        {
            
                //pContentRating.ContentRatingKey = Convert.ToInt32(pDbDataReader["ContentRatingKey"]);
                pContentRating.ContentRatingContentKey = Convert.ToInt32(pDbDataReader["ContentRatingContentKey"]);
                //pContentRating.ContentRatingValue = Convert.ToInt32(pDbDataReader["ContentRatingValue"]);
                //pContentRating.ContentRatingDate = Convert.ToDateTime(pDbDataReader["ContentRatingDate"]);
                //pContentRating.ContentRatingProviderUserKey = Convert.ToInt32(pDbDataReader["ContentRatingProviderUserKey"]);
                //pContentRating.ContentRatingProviderName = Convert.ToString(pDbDataReader["ContentRatingProviderName"]);
                //pContentRating.ContentRatingProviderEmail = Convert.ToString(pDbDataReader["ContentRatingProviderEmail"]);


                //DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContentRating, pDbDataReader);
            
        }
        #endregion Private Methods

    }
}
